/*
 * $Id: SyncItem.java 52 2008-02-13 03:40:02Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.syncml.protocol;

import org.w3c.dom.*;

public class SyncItem
{
	protected String mSourceId;
	protected String mTargetId;
	protected String mData;
	protected String mMimeType;
	protected boolean mMoreData = false;

	public SyncItem(Element item)
	{
		NodeList children = item.getChildNodes();

		for (int i = 0; i < children.getLength(); i++)
		{
			Node n = children.item(i);

			if (n.getNodeName().equals("Source") == true)
				mSourceId = ((Text)n.getFirstChild().getFirstChild()).getNodeValue();
			else if (n.getNodeName().equals("Target") == true)
				mTargetId = ((Text)n.getFirstChild().getFirstChild()).getNodeValue();
			else if (n.getNodeName().equals("Data") == true)
			{
				/* XXX: On Android, <Data> seems to be empty? */
				if (n.getFirstChild() != null)
					mData = ((Text)n.getFirstChild()).getNodeValue();
			}
			else if (n.getNodeName().equals("Meta") == true)
				mMimeType = ((Text)n.getFirstChild().getFirstChild()).getNodeValue();
			else if (n.getNodeName().equals("MoreData") == true)
				mMoreData = true;
		}
	}

	public void prependData(String data)
	{
		mData = data + mData;
	}

	public String getData()
	{
		return mData;
	} 

	public boolean hasMoreData()
	{
		return mMoreData;
	}

	public String getSourceId()
	{
		return mSourceId;
	}

	public String getTargetId()
	{
		return mTargetId;
	}

	public void setTargetId(String id)
	{
		mTargetId = id;
	}

	public void setTargetId(long id)
	{
		mTargetId = String.valueOf(id);
	}

	public void setMimeType(String type)
	{
		mMimeType = type;
	}

	public String getMimeType()
	{
		return mMimeType;
	}
}
